﻿namespace FTchina.Upgrade
{
    using System;
    using System.Configuration;
    using System.Data.Common;
    using System.IO;
    using System.Web;
    using System.Web.UI;

    public class Upgrade : Page
    {
        public void CreateFolder(string path)
        {
            if (!string.IsNullOrEmpty(path))
            {
                string[] strArray = path.Split(new char[] { '\\', '/' });
                if (strArray.Length > 0)
                {
                    string fileName = string.Empty;
                    for (int i = 0; i < strArray.Length; i++)
                    {
                        fileName = fileName = fileName + strArray[i];
                        if ((i + 1) < strArray.Length)
                        {
                            fileName = fileName + @"\";
                        }
                        try
                        {
                            FileInfo info = new FileInfo(fileName);
                            if (string.IsNullOrEmpty(info.Extension) && !Directory.Exists(fileName))
                            {
                                Directory.CreateDirectory(fileName);
                            }
                        }
                        catch
                        {
                        }
                    }
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string str = FTchina.Library.RequestHelper.GetMapPath("~");

            string path;
            string ver = "5";
            FTchina.Upgrade.UpgradeCheck check = new FTchina.Upgrade.UpgradeCheck(ver);
            string action = FTchina.basic.FilterSQL(Request["act"]);
            if (action != null)
            {
                if (!(action == "down"))
                {
                    if (action == "inst")
                    {
                        if (!string.IsNullOrEmpty(check.SQL))
                        {
                            string[] strArray = check.SQL.Split(new string[] { ";\n" }, StringSplitOptions.None);
                            //using (DbConnection connection = DbProviderFactories.GetFactory("System.Data.SQLite").CreateConnection())
                            //{
                            //    connection.ConnectionString = ConfigurationManager.ConnectionStrings["SQLite"].ConnectionString;
                            //    connection.Open();
                            //    using (DbTransaction transaction = connection.BeginTransaction())
                            //    {
                            //        using (DbCommand command = connection.CreateCommand())
                            //        {
                            //            command.Transaction = transaction;
                            //            foreach (string str5 in strArray)
                            //            {
                            //                command.CommandText = str5.Trim();
                            //                command.ExecuteNonQuery();
                            //            }
                            //        }
                            //        transaction.Commit();
                            //    }
                            //}
                        }
                        foreach (FileItem item in check.GetFileList)
                        {
                            path = Path.Combine(str, "Upgrade/" + ver + "/" + item.FilePath);
                            this.CreateFolder(Path.Combine(str, item.FilePath));
                            File.Copy(path, Path.Combine(str, item.FilePath), true);
                        }
                        foreach (FileItem item in check.GetRemoveList)
                        {
                            string fileName = Path.Combine(str, item.FilePath);
                            FileInfo info = new FileInfo(fileName);
                            if (info.Attributes == FileAttributes.Directory)
                            {
                                Directory.Delete(fileName, true);
                            }
                            else
                            {
                                File.Delete(fileName);
                            }
                        }
                        if (Directory.Exists(Path.Combine(str, "Upgrade")))
                        {
                            Directory.Delete(Path.Combine(str, "Upgrade"), true);
                        }
                    }
                }
                else
                {
                    foreach (FileItem item in check.GetFileList)
                    {
                        path = Path.Combine(str, "Upgrade/" + ver + "/" + item.FilePath);
                        this.CreateFolder(path);
                        new UpgradeDown(ver, item.FilePath, path).Down();
                    }
                }
            }

            HttpContext.Current.Response.Write("1");
        }

    }
}

